// Time:  O(n * l)
// Space: O(1)

#include <vector>
#include <cassert>

using namespace std;

class Solution {
public:
    vector<string> commonChars(vector<string>& words) {
        vector<int> alphabet(26, numeric_limits<int>::max());
        vector<string> result;
        for(const auto& word: words) {
            vector<int> tmp_alphabet(alphabet.size());
            for(const auto& c: word) {
                ++tmp_alphabet[c - 'a'];
            }
            for(size_t i=0; i<alphabet.size(); i++) {
                alphabet[i] = min(alphabet[i], tmp_alphabet[i]);
            }
        }
        for(size_t i=0; i<alphabet.size(); i++) {
            for(int j=0; j<alphabet[i]; j++) {
                result.emplace_back(1, i + 'a');
            }
        }
        return result;
    }
};

int main() {
    vector<string> words = {"bella", "label", "roller"};
    Solution sol;
    auto res = sol.commonChars(words);
    auto expected = vector<string>{"e", "l", "l"};
    assert(res == expected);
    return 0;
}
